package com.yiyuan.app.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

public class ImageUtils {


    public static void addTextToImage(String imagePath, String text, OutputStream outputStream, Color color, Integer fontSize, Integer x, Integer y) throws IOException {
        BufferedImage img = ImageIO.read(new File(imagePath));
        Graphics2D g2d = (Graphics2D) img.getGraphics();

        // 设置文字属性
        Font font = new Font("Arial", Font.BOLD, fontSize); // 字体、样式和大小
        g2d.setFont(font);
        g2d.setColor(color); // 文字颜色
        g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // 抗锯齿

        // 在图片上绘制文字
        g2d.drawString(text, x, y);

        // 保存新图片
        ImageIO.write(img, "png", outputStream);

        // 清理资源
        g2d.dispose();
    }
}
